package com.leiy.job.service.impl;

import com.leiy.job.Pojo.JobInfo;
import com.leiy.job.dao.JobInfoDao;
import com.leiy.job.service.JobInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class JobInfoServiceImpl implements JobInfoService {

    @Autowired
    private JobInfoDao jobInfoDao;
    /**
     * 根据条件查询所有记录
     *
     * @param jobInfo
     * @return
     */
    @Override
    public List<JobInfo> findAll(JobInfo jobInfo) {
        Example example = Example.of(jobInfo);
        return this.jobInfoDao.findAll(example);
    }

    /**
     * 保存，新增和更新
     * 开启事务
     * @param jobInfo
     */
    @Override
    //@Transactional  // 开启事务
    public void save(JobInfo jobInfo) {
        //先从数据库查询数据,根据发布日期查询和url查询
        JobInfo params = new JobInfo();
        params.setUrl(jobInfo.getUrl());
        params.setTime(jobInfo.getTime());
        Example example = Example.of(params);
        List<JobInfo> list = this.jobInfoDao.findAll(example);
        if(list.size() == 0){
            this.jobInfoDao.save(jobInfo);
        }
        //this.jobInfoDao.save(jobInfo);
    }
}
